//
//  PopoverAnimator.swift
//  swiftTest
//
//  Created by LiTing on 17/6/13.
//  Copyright © 2017年 LiTing. All rights reserved.
//

import UIKit

class PopoverAnimator: NSObject {
    //对外提供的属性
    var isPresented : Bool = false
    var presentedFrame :CGRect = CGRectZero
    var callBack : ((presented : Bool)-> ())?
    
    //MARK:- 自定义构造函数
    //注意：如果自定义了一个构造函数，但是没有对父类的构造函数init（）进行重写，那么自定义的构造函数会覆盖默认的init（）构造函数
    init(callBack : (presented : Bool)-> ()) {
        self.callBack = callBack
    }

}

//MARK:- 自定义转场代理的方法
extension PopoverAnimator : UIViewControllerTransitioningDelegate {
    //目的：改变弹出view的尺寸
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
        let presentation = XMGPresentationController(presentedViewController: presented, presentingViewController: presenting)
        presentation.presentedFrame = presentedFrame
        return presentation
    }
    
    //目的：自定义弹出的动画
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = true
        callBack!(presented : isPresented)
        return self
    }
    
    //目的：自定义弹出的动画
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = false
        callBack!(presented : isPresented)
        return self
    }
}

//MARK:- 弹出和消失动画代理的方法
extension PopoverAnimator : UIViewControllerAnimatedTransitioning {
    ///动画执行的时间
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.5
    }
    
    /// 获取“转场的上下文”：可以通过转场上下文获取弹出的view和消失的view
    // UITransitionContextFromViewKey : 获取消失的view
    // UITransitionContextToViewKey : 获取弹出的view
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        
        isPresented ? animationForPresentedView(transitionContext) : animationForDismissView(transitionContext)
    }
    
    ///自定义弹出动画
    private func animationForPresentedView(transitionContext: UIViewControllerContextTransitioning) {
        //1.获取弹出的view
        let presentedView = transitionContext.viewForKey(UITransitionContextToViewKey)!
        
        //2.将弹出的view添加到containerView中
        transitionContext.containerView()?.addSubview(presentedView)
        
        //3.执行动画
        presentedView.transform = CGAffineTransformMakeScale(1.0, 0.0)
        presentedView.layer.anchorPoint = CGPointMake(0.5, 0)
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: {
            
            presentedView.transform = CGAffineTransformIdentity
            
        }) { (_) in
            //必须告诉转场上下文你已经完成动画
            transitionContext.completeTransition(true)
        }
        
    }
    ///自定义消失动画
    private func animationForDismissView(transitionContext: UIViewControllerContextTransitioning) {
        //1.获取消失的view
        let dismissView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
        
        //2.执行动画
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: {
            
            dismissView.transform = CGAffineTransformMakeScale(1.0, 0.00001)
            
        }) { (_) in
            dismissView.removeFromSuperview()
            //必须告诉转场上下文你已经完成动画
            transitionContext.completeTransition(true)
        }
        
    }
}